These are instructions on how to use the functions in the libary psrc_census.R
The code requires several libraries. The config file contains global values specific to PSRC. The main code is psrc_census.R.
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.0.5
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.3 v purrr 0.3.4
## v tibble 3.1.1 v dplyr 1.0.5
## v tidyr 1.1.3 v stringr 1.4.0
## v readr 1.4.0 v forcats 0.5.1
## Warning: package 'tibble' was built under R version 4.0.5
## Warning: package 'tidyr' was built under R version 4.0.5
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(leaflet)
library(tidycensus)
## Warning: package 'tidycensus' was built under R version 4.0.5
source('../library/psrc_census_config.R')
source('../library/psrc_census.R')
The first time you run this code, you will need to set our Census API Key as an environment variable, if you haven’t doen taht. After that you can just get it. This is the website to get a key: https://api.census.gov/data/key_signup.html. Once you run Sys.setenv on the Census API Key you will only need to run Sys.getenv.
#Sys.setenv(CENSUS_API_KEY = 'PUT YOUR KEY HERE')
Sys.getenv("CENSUS_API_KEY")
Next you need to decide what tables you would like to download. This is the hardest part because you have find the correct table code, decide on geography, and which years. The main function psrc_acs_table has four parameters: tbl_code, geog, yr,acs.
You can find the list of ACS datasets available via the api here: https://www.census.gov/data/developers/data-sets.html
#psrc_table(tbl_code, geog, yr,acs)
tract_asian<-psrc_acs_table("B02001_005", "tract", 2019,'acs5')
## Getting data from the 2015-2019 5-year ACS
## # A tibble: 1,458 x 8
## GEOID NAME variable estimate moe ACS_Year ACS_Type ACS_Geography
## <chr> <chr> <chr> <dbl> <dbl> <dbl> <chr> <chr>
## 1 530019~ Census Tract~ B02001_~ 31 28 2019 acs5 tract
## 2 530019~ Census Tract~ B02001_~ 2 4 2019 acs5 tract
## 3 530019~ Census Tract~ B02001_~ 0 17 2019 acs5 tract
## 4 530019~ Census Tract~ B02001_~ 102 67 2019 acs5 tract
## 5 530019~ Census Tract~ B02001_~ 19 31 2019 acs5 tract
## 6 530039~ Census Tract~ B02001_~ 46 48 2019 acs5 tract
## 7 530039~ Census Tract~ B02001_~ 0 12 2019 acs5 tract
## 8 530039~ Census Tract~ B02001_~ 11 17 2019 acs5 tract
## 9 530039~ Census Tract~ B02001_~ 26 32 2019 acs5 tract
## 10 530039~ Census Tract~ B02001_~ 74 48 2019 acs5 tract
## # ... with 1,448 more rows
## # A tibble: 776 x 8
## GEOID NAME variable estimate moe ACS_Year ACS_Type ACS_Geography
## <chr> <chr> <chr> <dbl> <dbl> <dbl> <chr> <chr>
## 1 530330~ Census Tract~ B02001_~ 1202 248 2019 acs5 tract
## 2 530330~ Census Tract~ B02001_~ 1168 232 2019 acs5 tract
## 3 530330~ Census Tract~ B02001_~ 484 155 2019 acs5 tract
## 4 530330~ Census Tract~ B02001_~ 970 418 2019 acs5 tract
## 5 530330~ Census Tract~ B02001_~ 465 194 2019 acs5 tract
## 6 530330~ Census Tract~ B02001_~ 278 143 2019 acs5 tract
## 7 530330~ Census Tract~ B02001_~ 1094 355 2019 acs5 tract
## 8 530330~ Census Tract~ B02001_~ 997 254 2019 acs5 tract
## 9 530330~ Census Tract~ B02001_~ 412 195 2019 acs5 tract
## 10 530330~ Census Tract~ B02001_~ 323 64 2019 acs5 tract
## # ... with 766 more rows
county_ferry<-psrc_acs_table("B08006_013", "county", 2019, 'acs1')
## The 1-year ACS provides data for geographies with populations of 65,000 and greater.
## Getting data from the 2019 1-year ACS
## # A tibble: 20 x 8
## GEOID NAME variable estimate moe ACS_Year ACS_Type ACS_Geography
## <chr> <chr> <chr> <dbl> <dbl> <dbl> <chr> <chr>
## 1 53005 Benton County B08006_0~ NA NA 2019 acs1 county
## 2 53007 Chelan County B08006_0~ NA NA 2019 acs1 county
## 3 53009 Clallam County B08006_0~ NA NA 2019 acs1 county
## 4 53011 Clark County B08006_0~ 0 210 2019 acs1 county
## 5 53015 Cowlitz County B08006_0~ NA NA 2019 acs1 county
## 6 53021 Franklin Coun~ B08006_0~ NA NA 2019 acs1 county
## 7 53025 Grant County B08006_0~ NA NA 2019 acs1 county
## 8 53027 Grays Harbor ~ B08006_0~ NA NA 2019 acs1 county
## 9 53029 Island County B08006_0~ 201 161 2019 acs1 county
## 10 53033 King County B08006_0~ 1687 854 2019 acs1 county
## 11 53035 Kitsap County B08006_0~ 9788 2076 2019 acs1 county
## 12 53041 Lewis County B08006_0~ NA NA 2019 acs1 county
## 13 53045 Mason County B08006_0~ NA NA 2019 acs1 county
## 14 53053 Pierce County B08006_0~ 118 124 2019 acs1 county
## 15 53057 Skagit County B08006_0~ 22 27 2019 acs1 county
## 16 53061 Snohomish Cou~ B08006_0~ 61 106 2019 acs1 county
## 17 53063 Spokane County B08006_0~ 46 77 2019 acs1 county
## 18 53067 Thurston Coun~ B08006_0~ 0 210 2019 acs1 county
## 19 53073 Whatcom County B08006_0~ 85 99 2019 acs1 county
## 20 53077 Yakima County B08006_0~ 0 210 2019 acs1 county
## # A tibble: 4 x 8
## GEOID NAME variable estimate moe ACS_Year ACS_Type ACS_Geography
## <chr> <chr> <chr> <dbl> <dbl> <dbl> <chr> <chr>
## 1 53033 King County B08006_0~ 1687 854 2019 acs1 county
## 2 53035 Kitsap County B08006_0~ 9788 2076 2019 acs1 county
## 3 53053 Pierce County B08006_0~ 118 124 2019 acs1 county
## 4 53061 Snohomish Coun~ B08006_0~ 61 106 2019 acs1 county
create_tract_map, we send in a data frame by tract, and it makes a map.
create_tract_map(tract_asian)
## Reading layer `dbo.tract2010_nowater' from data source `MSSQL:server=AWS-PROD-SQL\Sockeye;database=ElmerGeo;trusted_connection=yes' using driver `MSSQLSpatial'
## Simple feature collection with 773 features and 19 fields
## geometry type: MULTIPOLYGON
## dimension: XY
## bbox: xmin: 1099353 ymin: -97548.53 xmax: 1622631 ymax: 477101.5
## projected CRS: NAD83 / Washington North (ftUS)